package com.ruoyi.project.custom.service.impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.custom.domain.CustomGroup;
import com.ruoyi.project.custom.mapper.CustomGroupMapper;
import com.ruoyi.project.custom.service.ICustomGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-06-26
 */
@Service
public class CustomGroupServiceImpl implements ICustomGroupService 
{
    @Autowired
    private CustomGroupMapper customGroupMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public CustomGroup selectCustomGroupById(Long id)
    {
        return customGroupMapper.selectCustomGroupById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param customGroup 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<CustomGroup> selectCustomGroupList(CustomGroup customGroup)
    {
        return customGroupMapper.selectCustomGroupList(customGroup);
    }

    @Override
    public String checkGroupNameUnique(CustomGroup group){
        Long groupId = StringUtils.isNull(group.getId()) ? -1L : group.getId();
        CustomGroup info = customGroupMapper.checkGroupNameUnique(group.getGroupName(), group.getParentId());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != groupId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    public int hasChildByGroupId(Long groupId){
        return customGroupMapper.hasChildByGroupId(groupId);
    }

    @Override
    public int checkExistNormalChildren(CustomGroup customGroup) {
        return customGroupMapper.checkExistNormalChildren(customGroup);
    }

    @Override
    public int checkExistUnNormalParent(CustomGroup customGroup) {
        return customGroupMapper.checkExistUnNormalParent(customGroup);
    }

    @Override
    public Long[] selectAllParentId(Long id) {
        List<Long> idList = new ArrayList<>();
        do{
            CustomGroup group = customGroupMapper.selectCustomGroupById(id);
            if(group != null){
                idList.add(id);
                id = group.getParentId();
            }
        }while (id != 0);

        Collections.reverse(idList);

        return idList.toArray(new Long[0]);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param customGroup 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertCustomGroup(CustomGroup customGroup)
    {
        CustomGroup group = customGroupMapper.selectCustomGroupById(customGroup.getParentId());
        if(group == null){
            customGroup.setFullGroupName(customGroup.getGroupName());
        }else{
            customGroup.setFullGroupName(group.getFullGroupName() + "/" + customGroup.getGroupName());
        }
        customGroup.setCreateBy(SecurityUtils.getLoginUser().getUser().getUserId().toString());
        customGroup.setUpdateBy(SecurityUtils.getLoginUser().getUser().getUserId().toString());
        return customGroupMapper.insertCustomGroup(customGroup);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param customGroup 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateCustomGroup(CustomGroup customGroup)
    {
        CustomGroup group = customGroupMapper.selectCustomGroupById(customGroup.getParentId());
        if(group == null){
            customGroup.setFullGroupName(customGroup.getGroupName());
        }else{
            customGroup.setFullGroupName(group.getFullGroupName() + "/" + customGroup.getGroupName());
        }
        customGroup.setUpdateBy(SecurityUtils.getLoginUser().getUser().getUserId().toString());
        return customGroupMapper.updateCustomGroup(customGroup);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteCustomGroupByIds(Long[] ids)
    {
        return customGroupMapper.deleteCustomGroupByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteCustomGroupById(Long id)
    {
        return customGroupMapper.deleteCustomGroupById(id);
    }
}
